using UnityEngine;

namespace BasicScript.Component
{
	public class FrameAnimation : MonoBehaviour
	{
		private SpriteRenderer sprite;

		public int curFrame;

		private int maxFrame;

		private bool isReversed;

		private float timer;

		public AtlasData assets;

		public float interval = 0.2f;

		public bool isLoop;

		public bool isPlaying;

		private void Awake()
		{
			sprite = base.transform.GetComponent<SpriteRenderer>();
			maxFrame = assets.sprites.Count - 1;
		}

		private void Update()
		{
			if (!isPlaying)
			{
				return;
			}
			timer += Time.deltaTime;
			if (!(timer >= interval))
			{
				return;
			}
			timer = 0f;
			if (isReversed)
			{
				curFrame--;
				if (isLoop)
				{
					if (curFrame < 0)
					{
						curFrame = maxFrame;
					}
				}
				else if (curFrame <= 0)
				{
					curFrame = 0;
					isPlaying = false;
				}
			}
			else
			{
				curFrame++;
				if (isLoop)
				{
					if (curFrame > maxFrame)
					{
						curFrame = 0;
					}
				}
				else if (curFrame >= maxFrame)
				{
					curFrame = maxFrame;
					isPlaying = false;
				}
			}
			UpdateFrame();
		}

		public void Continue()
		{
			isPlaying = true;
		}

		public void Stop()
		{
			isPlaying = false;
		}

		public void JumpToFrame(int val)
		{
			if (val < 0)
			{
				curFrame = 0;
			}
			else if (val > maxFrame)
			{
				curFrame = maxFrame;
			}
			else
			{
				curFrame = val;
			}
			UpdateFrame();
		}

		public void Play()
		{
			isPlaying = true;
			curFrame = 0;
			isReversed = false;
		}

		public void Play(bool isLoop)
		{
			isPlaying = true;
			curFrame = maxFrame;
			this.isLoop = isLoop;
		}

		public void IsLoop(bool val)
		{
			isLoop = val;
		}

		public void PlayReversed()
		{
			isPlaying = true;
			isReversed = true;
		}

		private void UpdateFrame()
		{
			sprite.sprite = assets.sprites[curFrame];
		}

		public void GotoFirstFrame()
		{
			curFrame = 0;
			UpdateFrame();
		}

		public void GotoLastFrame()
		{
			curFrame = maxFrame;
			UpdateFrame();
		}
	}
}
